#include "MainTestScene.h"

#include "NetworkManager.h"

MainTestSceneMainLayer::MainTestSceneMainLayer() :
    dragonite_(nullptr), fire_(nullptr)
{
}

MainTestSceneMainLayer::~MainTestSceneMainLayer()
{
    CC_SAFE_RELEASE(fire_);
}

bool MainTestSceneMainLayer::init()
{
    auto texture_cache = cocos2d::Director::getInstance()->getTextureCache();

    dragonite_ = cocos2d::Sprite::create("dragonite.png");
    addChild(dragonite_, 0);
    dragonite_->setPosition(200, 200);

    fire_ = cocos2d::ParticleFire::create();
    fire_->retain();
    fire_->setTexture(texture_cache->addImage("fire.png"));
    addChild(fire_, 10);
    fire_->setPosition(200, 200);

    return true;
}

void MainTestSceneMainLayer::onEnter()
{
    cocos2d::Layer::onEnter();

    auto action_move = cocos2d::MoveBy::create(2.0f,
        cocos2d::Point(100.0f, 100.0f));
    auto action_move_back = action_move->reverse();
    auto rep = cocos2d::RepeatForever::create(cocos2d::Sequence::create(
        action_move, action_move_back, NULL));

    dragonite_->runAction(rep);
    fire_->runAction(rep->clone());

    //NetworkManager::getInstance()->connect("ws://echo.websocket.org");
}


///////////////////////////////////////////////////////////////////////////////
MainTestScene::MainTestScene() :
    main_layer_(nullptr)
{
}

MainTestScene::~MainTestScene()
{
}

bool MainTestScene::init()
{
    main_layer_ = MainTestSceneMainLayer::create();
    if (!main_layer_) {
        CCLOG("create MainTestSceneMainLayer failed");
        return false;
    }
    addChild(main_layer_, 0);

    return true;
}
